﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business_Objects;
using ServicesComponent.Repositories;

public partial class QuanLyHocSinh_DiemDanh : System.Web.UI.Page
{
    public int id = 1;
    private IDiemDanhRepository _DiemDanhRepository;
    private IClassRepository _ClassRepository;
    protected void Page_Load(object sender, EventArgs e)
    {
        id = id + 1;
        if (!IsPostBack)
        {
            if (Request.QueryString["Refresh"] != null)
            {
                _DiemDanhRepository = FactoryServices.GetServicesDiemDanh(_DiemDanhRepository);
                //Bind();
                return;
            }
            if (Request.QueryString["Page"] != null)
            {
                id = (int.Parse(Request.QueryString["Page"].ToString()) - 1) * 14 + 1;
            }

            //trong IsPostBack(combobox tron lại mạc định )
        GetDataToCombobox();
        GetDataRepeatEdit(ddlKhoi.SelectedValue);
        }
        //Ngoài IsPostBack
        _DiemDanhRepository = FactoryServices.GetServicesDiemDanh(_DiemDanhRepository);
        _ClassRepository = FactoryServices.GetServicesClass(_ClassRepository);

      //  Bind();
    }
    private void GetDataToCombobox()
    {
        _ClassRepository = FactoryServices.GetServicesClass(_ClassRepository);
        ddlKhoi.DataSource = _ClassRepository.GetList();
        ddlKhoi.DataValueField = "Id";
        ddlKhoi.DataTextField = "Name";
        ddlKhoi.DataBind();
    }
    void GetDataRepeatEdit(string classId)
    {
        _DiemDanhRepository = FactoryServices.GetServicesDiemDanh(_DiemDanhRepository);
        rptA.DataSource = _DiemDanhRepository.GetBienLaiThuByClassIdAndSchoolSchoolYear(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()), classId);

        rptA.DataBind();

        Cpage.DataSource = _DiemDanhRepository.GetBienLaiThuByClassIdAndSchoolSchoolYear(Session["SchoolYearID"].ToString(),
                                                                     int.Parse(Session["SchoolID"].ToString()), classId);
        Cpage.BindToControl = rptA;
        rptA.DataSource = Cpage.DataSourcePaged;
    }
    //get add = Edit - Cùng fill student in class
    //Delete Recall 
    //void Bind()
    //{
    //    rptA.Dispose();
    //    // rptA.RenderControl("f");
    //    rptA.DataSource = null;
    //    //   rptA.BeginResetItemTemplate();
    //    //Truyền @ClassID="";
    //    rptA.DataSource = _DiemDanhRepository.GetDiemDanhBySchoolIdAndSchoolYear(Session["SchoolYearID"].ToString(), int.Parse(Session["SchoolID"].ToString()));
    //    // rptA.DataSource = _ClassRepository.GetClassPhanLopBySchoolIdAndSchoolYearID(Session["SchoolYearID"].ToString(), int.Parse(Session["SchoolID"].ToString()));
    //    rptA.DataBind();
    //    Cpage.DataSource = _DiemDanhRepository.GetDiemDanhBySchoolIdAndSchoolYear(Session["SchoolYearID"].ToString(), int.Parse(Session["SchoolID"].ToString()));
    //    // Cpage.DataSource = _ClassRepository.GetClassPhanLopBySchoolIdAndSchoolYearID(Session["SchoolYearID"].ToString(), int.Parse(Session["SchoolID"].ToString()));
    //    Cpage.BindToControl = rptA;
    //    rptA.DataSource = Cpage.DataSourcePaged;
    //    // drAddress.EndResetItemTemplate()

    //}

    protected void ddlKhoi_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetDataRepeatEdit(ddlKhoi.SelectedValue);
    //    txtSiSo.Text = rptA.Items.Count.ToString();
        //txtDenLop.Value = txtSiSo.Value;
        //txtVang.Value = "0";

    }
    public string GetTenLopById(string id)
    {
        Class obj = _ClassRepository.Get(id);
        return obj.Name;
    }
    public string ConVertDate(DateTime startDate)
    {
        return startDate.ToShortDateString();
    }
}